package fhp.hlhj.giantfold.activity.user

import android.annotation.SuppressLint
import android.content.Intent
import android.support.v7.app.AlertDialog
import com.alibaba.baichuan.trade.biz.login.AlibcLogin
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.interfaces.ISettings
import fhp.hlhj.giantfold.javaBean.LoginOutBean
import fhp.hlhj.giantfold.presenter.SettingsPresenter
import fhp.hlhj.giantfold.utils.DataCleanManager
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.SPUtils
import kotlinx.android.synthetic.main.activity_settings_aty.*
import com.kepler.jd.login.KeplerApiManager
import com.kepler.jd.Listener.LoginListener
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.activity.malls.JdCodeAty
import fhp.hlhj.giantfold.customView.DownloadDialog
import fhp.hlhj.giantfold.interfaces.IVersion
import fhp.hlhj.giantfold.javaBean.VersionBean
import fhp.hlhj.giantfold.presenter.VersionPresenter
import fhp.hlhj.giantfold.utils.MyUtils


class SettingsAty : BaseActivity() , ISettings,IVersion {
    private lateinit var settingsPresenter:SettingsPresenter
    private lateinit var verSionPresenter:VersionPresenter
    override fun getContentId(): Int = R.layout.activity_settings_aty
    override fun initView() {
        tvAche.text=DataCleanManager.getTotalCacheSize(this)
        settingsPresenter= SettingsPresenter(this)

        verSionPresenter= VersionPresenter(this)

        versionName.text=MyUtils.getVersion(this)

        if (SPUtils.getTaoBaoLogin(this)==1){
            switchTaoBao.isChecked=true
        }else{
            switchTaoBao.isChecked=false
        }
    }
    @SuppressLint("SetJavaScriptEnabled")
    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        /*清理缓存*/
        btClear.setOnClickListener {
           AlertDialog.Builder(this).setTitle("清理缓存？")
                   .setPositiveButton("确定") { dialog, which ->
                        DataCleanManager.clearAllCache(this)
                       tvAche.text=DataCleanManager.getTotalCacheSize(this)
                   }
                   .setNegativeButton("取消"){
                       dialog,_->
                       dialog.dismiss()
                   }
                   .create().show()
        }
        /*登出*/
        btLoginOut.setOnClickListener {
            AlertDialog.Builder(this).setTitle("确认退出？")
                    .setPositiveButton("确定"){
                        dialog, which ->
                        Contents.userId="0"
                        SPUtils.saveLoginType(this,-1)
                        settingsPresenter.loginOut()
                        setResult(-1)
                        finish()
                    }
                    .setNegativeButton("取消"){
                        dialog, which ->
                        dialog.dismiss()
                    }
                    .create().show()
        }
        /*绑定手机*/
        loPhone.setOnClickListener {
            val intent = Intent(this, BindPhoneAty::class.java)
            startActivity(intent)
        }

        loTaoBao.setOnClickListener {

            AlertDialog.Builder(this).setTitle("确认操作")
                    .setPositiveButton("确认"){
                        dialog, which ->
                        if (switchTaoBao.isChecked){
                            val alibcLogin = AlibcLogin.getInstance()
                            alibcLogin.logout(object :AlibcLoginCallback{
                                override fun onSuccess(p0: Int) {
                                    SPUtils.saveTaoBaoLogin(this@SettingsAty,0)
                                    switchTaoBao.isChecked=false
                                }

                                override fun onFailure(p0: Int, p1: String?) {
                                    MyUtils.toast("操作失败")
                                }
                            })
                        }else if (!switchTaoBao.isChecked){
                            val alibcLogin = AlibcLogin.getInstance()
                            alibcLogin.showLogin(object :AlibcLoginCallback{
                                override fun onSuccess(p0: Int) {
                                    LogUtil.log("登陆陈宫"+AlibcLogin.getInstance().session)
                                    switchTaoBao.isChecked=true
                                    SPUtils.saveTaoBaoLogin(this@SettingsAty,1)
                                }

                                override fun onFailure(p0: Int, p1: String?) {
                                    LogUtil.log("登录失败")
                                    MyUtils.toast("登录失败")
                                }

                            })
                        }

                        dialog.dismiss()
                    }
                    .setNegativeButton("取消"){
                        dialog, which ->
                        dialog.dismiss()
                    }.show()

        }
        val mLoginListener = object : LoginListener<String> {
            override fun authSuccess(token: String) {
                LogUtil.log("授权陈宫${token.toString()}")
                SPUtils.saveJDToken(this@SettingsAty,token)
            }
            override fun authFailed(errorCode: Int) {
                LogUtil.log("登录失败$errorCode")
                // TODO Auto-generated method stub
                when (errorCode) {
                    KeplerApiManager.KeplerApiManagerLoginErr_Init// 初始化失败 break;
                        , KeplerApiManager.KeplerApiManagerLoginErr_InitIng// 初始化没有完成 break;
                        , KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull//跳转url // 为null break;
                        , KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                        , KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel// 用户取消 break;
                        , KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen//打开授权页面失败 break;
                    -> {
                    }
                    else -> {
                    }
                }
            }
        }
        loJD.setOnClickListener {
            startActivity(Intent(this,JdCodeAty::class.java))
            /*LogUtil.log("登录没有呀${KeplerApiManager.getWebViewService().isKeplerLogined}")
            if (KeplerApiManager.getWebViewService().isKeplerLogined){
                KeplerApiManager.getWebViewService().login(this, mLoginListener)
            }else{
                KeplerApiManager.getWebViewService().login(this,object:LoginListener<String>{
                    override fun authSuccess(p0: String?) {
                        LogUtil.log("授权陈宫${p0.toString()}")
                    }

                    override fun authFailed(p0: Int) {
                        LogUtil.log("登录失败$p0")
                    }
                })

            }*/
        }

        btUpdata.setOnClickListener {
            verSionPresenter.getVersion()
        }


    }
    override fun loginOut(loginOutBean: LoginOutBean) {

    }

    override fun showLoading() {
    }

    override fun getVersion(versionBean: VersionBean) {
        if (versionBean.code==200){
            if (versionBean.data.version.equals(MyUtils.getVersion(this))){
                AlertDialog.Builder(this).setTitle("更新")
                        .setMessage("已经是最新的啦")
                        .setPositiveButton("确定"){
                            dialog, which ->
                            dialog.dismiss()
                        }
                        .setNegativeButton("取消"){
                            dialog, which ->
                            dialog.dismiss()
                        }.show()
            }else{
                android.app.AlertDialog.Builder(this).setTitle("版本更新")
                        .setMessage(versionBean.data.description)
                        .setPositiveButton("更新"){
                            dialog, which ->
                            DownloadDialog(this,versionBean.data.savename,this).show()
                        }
                        .setNegativeButton("取消"){
                            dialog, which ->
                            dialog.dismiss()
                        }
                        .show()
            }
        }
    }
}
